package org.jeecg.modules.basicShelfType.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.basicShelfType.entity.BasicShelfType;
import org.jeecg.modules.basicShelfType.service.IBasicShelfTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 货架类型
 * @Author: jeecg-boot
 * @Date: 2023-11-01
 * @Version: V1.0
 */
@Api(tags = "货架类型")
@RestController
@RequestMapping("/basicShelfType/basicShelfType")
@Slf4j
public class BasicShelfTypeController extends JeecgController<BasicShelfType, IBasicShelfTypeService> {
    @Autowired
    private IBasicShelfTypeService basicShelfTypeService;

    /**
     * 分页列表查询
     *
     * @param basicShelfType
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "货架类型-分页列表查询")
    @ApiOperation(value = "货架类型-分页列表查询", notes = "货架类型-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BasicShelfType basicShelfType,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BasicShelfType> queryWrapper = QueryGenerator.initQueryWrapper(basicShelfType, req.getParameterMap());
        Page<BasicShelfType> page = new Page<BasicShelfType>(pageNo, pageSize);
        IPage<BasicShelfType> pageList = basicShelfTypeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @GetMapping("/getAll")
    public Result<?> getAll() {
        List<BasicShelfType> list = basicShelfTypeService.list();
        return Result.OK(list);
    }

    /**
     *   添加
     *
     * @param basicShelfType
     * @return
     */
    @AutoLog(value = "货架类型-添加")
    @ApiOperation(value = "货架类型-添加", notes = "货架类型-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BasicShelfType basicShelfType) {
        this.checkColumns(basicShelfType);
        basicShelfTypeService.save(basicShelfType);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param basicShelfType
     * @return
     */
    @AutoLog(value = "货架类型-编辑")
    @ApiOperation(value = "货架类型-编辑", notes = "货架类型-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BasicShelfType basicShelfType) {
        this.checkColumns(basicShelfType);
        basicShelfTypeService.updateById(basicShelfType);
        return Result.OK("编辑成功!");
    }

    public void checkColumns(BasicShelfType basicShelfType) {
        Assert.isTrue(StringUtils.isNotBlank(basicShelfType.getShelfCode()), "请输入货架类型编码");
        Assert.isTrue(basicShelfType.getShelfCode().length() == 1, "货架类型编码只能输入一位");
        Assert.isTrue(StringUtils.isNotBlank(basicShelfType.getShelfName()), "请输入货架类型名称");
        Assert.isTrue(StringUtils.isNotBlank(basicShelfType.getWarehouseCode()), "请选择所属仓库");
        Assert.notNull(basicShelfType.getPlie(), "请输入货架层数");
        Assert.notNull(basicShelfType.getColums(), "请输入货架格数");
        Assert.notNull(basicShelfType.getIsTwoSided(), "请选择是否开启F/B面");

        QueryWrapper<BasicShelfType> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(null != basicShelfType.getId(), "id", basicShelfType.getId());
        queryWrapper.eq("shelf_code", basicShelfType.getShelfCode());
        int repeatCount = basicShelfTypeService.count(queryWrapper);
        Assert.isTrue(repeatCount == 0, "货架类型编码已存在");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "货架类型-通过id删除")
    @ApiOperation(value = "货架类型-通过id删除", notes = "货架类型-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        basicShelfTypeService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "货架类型-批量删除")
    @ApiOperation(value = "货架类型-批量删除", notes = "货架类型-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.basicShelfTypeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "货架类型-通过id查询")
    @ApiOperation(value = "货架类型-通过id查询", notes = "货架类型-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BasicShelfType basicShelfType = basicShelfTypeService.getById(id);
        Assert.notNull(basicShelfType, "货架类型数据异常,请联系管理员");
        return Result.OK(basicShelfType);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param basicShelfType
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BasicShelfType basicShelfType) {
        return super.exportXls(request, basicShelfType, BasicShelfType.class, "货架类型");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BasicShelfType.class);
    }

}
